home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unarj_s1.lzh / SOURCE.ST / ENVIRON.C < prev    next >
C/C++ Source or Header  |  1992-09-21  |  14KB  |  746 lines

  1. /* ENVIRON.C, UNARJ, R JUNG, 09/01/91
  2.  * Implementation dependent routines
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   The UNIX file date-time stamping code is derived from ZOO by
  18.  *   Rahul Dhesi.
  19.  *
  20.  * Modification history:
  21.  * Date      Programmer  Description of modification.
  22.  * 04/09/91  R. Jung     Rewrote code.
  23.  * 04/23/91  M. Adler    Portabilized.
  24.  * 04/29/91  R. Jung     Added get_mode_str().
  25.  * 05/08/91  R. Jung     Combined set_ftime() and set_fmode().
  26.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  27.  *                       set_ftime_mode().
  28.  * 07/07/91  R. Jung     Added default_case_path() and UNIX section.
  29.  * 07/24/91  R. Jung     Fixed use of _chmod to handle directories.
  30.  * 08/27/91  R. Jung     Added date/time handling to Coherent.
  31.  * 09/01/91  R. Jung     Added #include <stdlib.h> to vanilla section.
  32.  *                       Added file date-time stamping to UNIX section.
  33.  *
  34.  */
  35.  
  36. #include "unarj.h"
  37.  
  38. #ifdef __TURBOC__
  39.  
  40. #define SUBS_DEFINED
  41.  
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <tos.h>
  45. #include <ext.h>
  46.  
  47. FILE *
  48. file_open(name, mode)
  49. char *name;
  50. char *mode;
  51. {
  52.     return fopen(name, mode);
  53. }
  54.  
  55. int
  56. file_read(buf, size, nitems, stream)
  57. char *buf;
  58. int  size;
  59. int  nitems;
  60. FILE *stream;
  61. {
  62.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  63. }
  64.  
  65. int
  66. file_seek(stream, offset, mode)
  67. FILE *stream;
  68. long offset;
  69. int  mode;
  70. {
  71.     return fseek(stream, offset, mode);
  72. }
  73.  
  74. long
  75. file_tell(stream)
  76. FILE *stream;
  77. {
  78.     return ftell(stream);
  79. }
  80.  
  81. int
  82. file_write(buf, size, nitems, stream)
  83. char *buf;
  84. int  size;
  85. int  nitems;
  86. FILE *stream;
  87. {
  88.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  89. }
  90.  
  91. voidp *
  92. xmalloc(size)
  93. int size;
  94. {
  95.     return (voidp *)malloc((size_t) size);
  96. }
  97.  
  98. void
  99. case_path(name)
  100. char *name;
  101. {
  102.     strupper(name);
  103. }
  104.  
  105. void
  106. default_case_path(name)
  107. char *name;
  108. {
  109.     strupper(name);
  110. }
  111.  
  112. int
  113. file_exists(name)
  114. char *name;
  115. {
  116.     FILE *fd;
  117.  
  118.     if ((fd = fopen(name, "rb")) == NULL)
  119.         return 0;
  120.     fclose(fd);
  121.     return 1;
  122. }
  123.  
  124. void
  125. get_mode_str(str, mode)
  126. char *str;
  127. uint mode;
  128. {
  129.     strcpy(str, "---W");
  130.     if (mode & FA_ARCH)
  131.         str[0] = 'A';
  132.     if (mode & FA_SYSTEM)
  133.         str[1] = 'S';
  134.     if (mode & FA_HIDDEN)
  135.         str[2] = 'H';
  136.     if (mode & FA_RDONLY)
  137.         str[3] = 'R';
  138. }
  139.  
  140. int
  141. set_ftime_mode(name, tstamp, attribute, host)
  142. char  *name;
  143. ulong tstamp;
  144. uint  attribute;
  145. uint  host;
  146. {
  147.     FILE *fd;
  148.     int code;
  149.  
  150.     if ((fd = fopen(name, "r+b")) == NULL)
  151.         return -1;
  152.     code = setftime(fileno(fd), (struct ftime *) &tstamp);
  153.     fclose(fd);
  154.     if (host == OS)
  155.     {
  156.         attribute &= 0x27;
  157.         if (Fattrib(name, 1, attribute) == -1)
  158.             return -1;
  159.     }
  160.     return code;
  161. }
  162.  
  163. #endif
  164.  
  165. #ifdef _QC
  166.  
  167. #define SUBS_DEFINED
  168.  
  169. #include <string.h>
  170. #include <dos.h>
  171. #include <io.h>
  172. #include <fcntl.h>
  173. #include <malloc.h>
  174.  
  175. FILE *
  176. file_open(name, mode)
  177. char *name;
  178. char *mode;
  179. {
  180.     return fopen(name, mode);
  181. }
  182.  
  183. int
  184. file_read(buf, size, nitems, stream)
  185. char *buf;
  186. int  size;
  187. int  nitems;
  188. FILE *stream;
  189. {
  190.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  191. }
  192.  
  193. int
  194. file_seek(stream, offset, mode)
  195. FILE *stream;
  196. long offset;
  197. int  mode;
  198. {
  199.     return fseek(stream, offset, mode);
  200. }
  201.  
  202. long
  203. file_tell(stream)
  204. FILE *stream;
  205. {
  206.     return ftell(stream);
  207. }
  208.  
  209. int
  210. file_write(buf, size, nitems, stream)
  211. char *buf;
  212. int  size;
  213. int  nitems;
  214. FILE *stream;
  215. {
  216.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  217. }
  218.  
  219. voidp *
  220. xmalloc(size)
  221. int size;
  222. {
  223.     return (voidp *)malloc((size_t) size);
  224. }
  225.  
  226. void
  227. case_path(name)
  228. char *name;
  229. {
  230.     strupper(name);
  231. }
  232.  
  233. void
  234. default_case_path(name)
  235. char *name;
  236. {
  237.     strupper(name);
  238. }
  239.  
  240. int
  241. file_exists(name)
  242. char *name;
  243. {
  244.     return (access(name, 0) == 0);
  245. }
  246.  
  247. void
  248. get_mode_str(str, mode)
  249. char *str;
  250. uint mode;
  251. {
  252.     strcpy(str, "---W");
  253.     if (mode & FA_ARCH)
  254.         str[0] = 'A';
  255.     if (mode & FA_SYSTEM)
  256.         str[1] = 'S';
  257.     if (mode & FA_HIDDEN)
  258.         str[2] = 'H';
  259.     if (mode & FA_RDONLY)
  260.         str[3] = 'R';
  261. }
  262.  
  263. int
  264. set_ftime_mode(name, tstamp, attribute, host)
  265. char  *name;
  266. ulong tstamp;
  267. uint  attribute;
  268. uint  host;
  269. {
  270.     FILE *fd;
  271.     int code;
  272.     uint date_stamp, time_stamp;
  273.  
  274.     date_stamp = (uint)(tstamp >> 16);
  275.     time_stamp = (uint)(tstamp & 0xFFFF);
  276.     if ((fd = fopen(name, "r+b")) == NULL)
  277.         return -1;
  278.     code = _dos_setftime(fileno(fd), date_stamp, time_stamp);
  279.     fclose(fd);
  280.     if (host == OS)
  281.     {
  282.         if (_dos_setfileattr(name, attribute))
  283.             return -1;
  284.     }
  285.     return code;
  286. }
  287.  
  288. #endif
  289.  
  290. #ifdef _OS2
  291.  
  292. #define SUBS_DEFINED
  293.  
  294. #include <string.h>
  295. #define INCL_DOSFILEMGR
  296. #include <os2.h>
  297. #include <io.h>
  298. #include <fcntl.h>
  299.  
  300. FILE *
  301. file_open(name, mode)
  302. char *name;
  303. char *mode;
  304. {
  305.     return fopen(name, mode);
  306. }
  307.  
  308. int
  309. file_read(buf, size, nitems, stream)
  310. char *buf;
  311. int  size;
  312. int  nitems;
  313. FILE *stream;
  314. {
  315.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  316. }
  317.  
  318. int
  319. file_seek(stream, offset, mode)
  320. FILE *stream;
  321. long offset;
  322. int  mode;
  323. {
  324.     return fseek(stream, offset, mode);
  325. }
  326.  
  327. long
  328. file_tell(stream)
  329. FILE *stream;
  330. {
  331.     return ftell(stream);
  332. }
  333.  
  334. int
  335. file_write(buf, size, nitems, stream)
  336. char *buf;
  337. int  size;
  338. int  nitems;
  339. FILE *stream;
  340. {
  341.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  342. }
  343.  
  344. voidp *
  345. xmalloc(size)
  346. int size;
  347. {
  348.     return (voidp *)malloc((size_t) size);
  349. }
  350.  
  351. void
  352. case_path(name)
  353. char *name;
  354. {
  355.     strupper(name);
  356. }
  357.  
  358. void
  359. default_case_path(name)
  360. char *name;
  361. {
  362.     strupper(name);
  363. }
  364.  
  365. int
  366. file_exists(name)
  367. char *name;
  368. {
  369.     return (access(name, 0) == 0);
  370. }
  371.  
  372. void
  373. get_mode_str(str, mode)
  374. char *str;
  375. uint mode;
  376. {
  377.     strcpy(str, "---W");
  378.     if (mode & FA_ARCH)
  379.         str[0] = 'A';
  380.     if (mode & FA_SYSTEM)
  381.         str[1] = 'S';
  382.     if (mode & FA_HIDDEN)
  383.         str[2] = 'H';
  384.     if (mode & FA_RDONLY)
  385.         str[3] = 'R';
  386. }
  387.  
  388. int
  389. set_ftime_mode(name, tstamp, attribute, host)
  390. char  *name;
  391. ulong tstamp;
  392. uint  attribute;
  393. uint  host;
  394. {
  395.     int code;
  396.     FDATE date_stamp;
  397.     FTIME time_stamp;
  398.     HFILE handle;
  399.     FILESTATUS info;
  400.     USHORT action;
  401.  
  402.     date_stamp.day = ts_day (tstamp);
  403.     date_stamp.month = ts_month (tstamp);
  404.     date_stamp.year = ts_year (tstamp) - 1980;
  405.     time_stamp.twosecs = ts_sec (tstamp) / 2;
  406.     time_stamp.minutes = ts_min (tstamp);
  407.     time_stamp.hours = ts_hour (tstamp);
  408.     if (DosOpen (name, &handle, &action, 0L, 0, FILE_OPEN,
  409.                  OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L) != 0)
  410.         return -1;
  411.     info.fdateCreation = date_stamp;
  412.     info.ftimeCreation = time_stamp;
  413.     info.fdateLastAccess = date_stamp;
  414.     info.ftimeLastAccess = time_stamp;
  415.     info.fdateLastWrite = date_stamp;
  416.     info.ftimeLastWrite = time_stamp;
  417.     info.cbFile = 0;
  418.     info.cbFileAlloc = 0;
  419.     info.attrFile = 0;
  420.     code = (int)DosSetFileInfo (handle, 1, (PBYTE)&info, sizeof (info));
  421.     (void)DosClose (handle);
  422.     if (host == OS)
  423.     {
  424.         if (DosSetFileMode (name, attribute, 0L))
  425.             return -1;
  426.     }
  427.     return code;
  428. }
  429.  
  430. #endif
  431.  
  432. #ifdef UNIX
  433.  
  434. #define SUBS_DEFINED
  435.  
  436. #include <time.h>
  437.  
  438. #ifndef time_t
  439. #define time_t long
  440. #endif
  441.  
  442. extern struct tm *localtime();
  443. extern time_t time();
  444. extern char   *strcpy();
  445. extern voidp  *malloc();
  446.  
  447. FILE *
  448. file_open(name, mode)
  449. char *name;
  450. char *mode;
  451. {
  452.